<?php

include_once '../inc/db_util.php';

function view() {
    $array_category = array();
    $query = mysql_query("SELECT * FROM tb_kategori");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_kategori'];
        $row_array['Kategori'] = $row['kategori'];

        array_push($array_category, $row_array);
    }
    return json_encode($array_category);
}

function find($filter) {
    $array_category = array();
    $jsArray = json_decode($filter, false);
    $query = mysql_query("SELECT * FROM tb_kategori WHERE kategori LIKE '%" . $jsArray->{'Kategori'} . "%'");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_kategori'];
        $row_array['Kategori'] = $row['kategori'];

        array_push($array_category, $row_array);
    }
    return json_encode($array_category);
}

function create($json) {
    $jsArray = json_decode($json, false);
    $sql = "INSERT INTO tb_kategori(id_kategori, kategori)
        VALUES ('" . $jsArray->{'Id'} . "', '" . $jsArray->{'Kategori'} . "')";

    return mysql_query($sql);
}

function update($json) {
    $jsArray = json_decode($json, false);
	$sql = "UPDATE tb_kategori SET 
			kategori='" . $jsArray->{'Kategori'} . "'
			WHERE 
			id_kategori='" . $jsArray->{'Id'} . "'";

	return mysql_query($sql);
}

function delete($json) {
    $jsArray = json_decode($json, false);
    $sql = "DELETE FROM tb_kategori WHERE id_kategori='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}
?>
